#import "Settings.h"

#import <UserNotifications/UserNotifications.h>

@implementation IOSSettings

RCT_EXPORT_MODULE();

- (dispatch_queue_t) methodQueue{
  return dispatch_get_main_queue();
}

RCT_EXPORT_METHOD(isNotificationAllow:(RCTPromiseResolveBlock)resolve 
                             rejecter:(RCTPromiseRejectBlock)reject){
  if (@available(iOS 10.0, *)) {
    [[UNUserNotificationCenter currentNotificationCenter] 
          getNotificationSettingsWithCompletionHandler: ^(UNNotificationSettings * _Nonnull settings) {
            BOOL isAllow = (settings.authorizationStatus == UNAuthorizationStatusAuthorized);
            resolve(@(isAllow));
          }
    ];
  } else {
    UIUserNotificationSettings *settings = [[UIApplication sharedApplication] currentUserNotificationSettings];
    BOOL isAllow = (settings.types != UIUserNotificationTypeNone);
    resolve(@(isAllow));
  }
}

RCT_EXPORT_METHOD(openSettings){
  NSURL *url = [NSURL URLWithString: UIApplicationOpenSettingsURLString];
  if ([[UIApplication sharedApplication] canOpenURL: url]) {
    if (@available(iOS 10.0, *)) {
      [[UIApplication sharedApplication] openURL: url options: @{} completionHandler: ^(BOOL success) {}];
    }
  }
}

@end
